package com.bw.demo.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.demo.R
import com.bw.demo.adapter.TypeAdapter
import com.bw.demo.adapter.VideoAdapter
import com.bw.demo.base.BaseFragment
import com.bw.demo.databinding.FragmentHomeBinding
import com.bw.demo.entity.TypeEntity
import com.bw.demo.entity.VideoEntity
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer


class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    private lateinit var viewModel:HomeViewModel
    private lateinit var videoAdapter: VideoAdapter
    private lateinit var typeAdapter: TypeAdapter
    val listvideo= mutableListOf<VideoEntity>()
    val listtype= mutableListOf<TypeEntity>()
    override fun initData() {
        viewModel=ViewModelProvider(this)[HomeViewModel::class.java]
        viewModel.getVideo("94349546935",1,10)
        viewModel.getType(1,10)
        viewModel.success.observe(this){
            listvideo.clear()
            listvideo.addAll(it.data)
            videoAdapter.notifyDataSetChanged()
        }
        viewModel.success2.observe(this){
            listtype.clear()
            listtype.addAll(it.data)
            typeAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        mDataBinding.rec.layoutManager=LinearLayoutManager(context)
        videoAdapter= VideoAdapter(listvideo)
        mDataBinding.rec.adapter=videoAdapter



        mDataBinding.recType.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
        typeAdapter= TypeAdapter(listtype)
        mDataBinding.recType.adapter=typeAdapter

        typeAdapter.setOnItemClickListener { adapter, view, position ->
            val channelid = typeAdapter.data.get(position).channelid
            viewModel.getVideo(channelid,1,10)
        }
      videoAdapter.setOnItemClickListener { adapter, view, position ->
          val standardGSYVideoPlayer =
              videoAdapter.getViewByPosition(position, R.id.gsy) as StandardGSYVideoPlayer
          standardGSYVideoPlayer.setUp(listvideo[position].videopath,true,"")
          standardGSYVideoPlayer.startPlayLogic()
          GSYVideoType.setShowType(GSYVideoType.SCREEN_MATCH_FULL)
          val imimageView =
              videoAdapter.getViewByPosition(position, R.id.iv_video_bofang) as ImageView
          imimageView.visibility=View.GONE

      }
    }

    override fun getLyoutId(): Int {
        return R.layout.fragment_home
    }

}